var t = getApp();
var bmap = require('../../../../lib/bmap-wx.min.js');
Page({
  data: {
    position: "",
    show: !0,
    index: "",
    juli: '',
    text: '主治医师',
  },
  onLoad: function(options) {
    var that = this
    var zid = options.zid
    var arr = JSON.parse(options.arr)
    var t_id = arr.t_id
    var yao_id = options.yao_id
    var yao_type = options.yao_type
    if (yao_type == 0) {

      var button = 'tichu'
    }
    if (yao_type == 1) {
      var button = 'tichu'
    }
    if (yao_type == 2) {
      var button = 'yijujue'
    }
    if (yao_type == 3) {
      var button = 'tgsq'
    }
    t.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    this.loadInfo();
    var BMap = new bmap.BMapWX({
      ak: '4DGFO0htsrocLEd7iQefj7F9tL1Fw1Xn'
    });
    var fail = function(data) {
      console.log(data)
    };
    var success = function(data) {
      console.log(data)
      // wxMarkerData = data.wxMarkerData;
      wxMarkerData = that.data.array
      that.setData({
        markers: wxMarkerData,
      });
    }
    that.setData({
      zid: zid,
      arr: arr,
      yao_type: yao_type,
      button: button,
      yao_id: yao_id
    })
  },
  onShow: function() {},
  zhaozhuanjia: function(e) {
    var that = this
    var zid = that.data.zid
    var form_id = e.detail.formId
    var arr = that.data.arr
    var uniacid = t.siteInfo.uniacid;
    console.log(arr)
    t.util.request({
      'url': that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=UserFormId&m=hyb_yl',
      'data': {
        form_id: form_id,
        openid: wx.getStorageSync('openid'),
      },
      success: function(res) {
        console.log(res)
        //发出邀请
        t.util.request({
          'url': 'entry/wxapp/Yaoqing.addyihu',
          'data': {
            zid: zid,
            arr: arr,
          },
          success: function(res) {
            console.log(res)
          }
        })
        wx.showToast({
          title: "已成功发出邀请",
          icon: "none",
          duration: 2e3,
          success: function() {
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          }
        })
      }
    })
  },

  loadInfo: function() {
    var page = this
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
      success: function(res) {
        console.log(res)
        var longitude = res.longitude
        var latitude = res.latitude
        page.loadCity(longitude, latitude)
        page.getAllzhuanjia(longitude, latitude)
      },
      fail: function() {
        // fail  
      },
      complete: function() {
        // complete  
      }
    })
  },
  loadCity: function(longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=4DGFO0htsrocLEd7iQefj7F9tL1Fw1Xn&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        var city = res.data.result.addressComponent.city;
        //查询当前城市的所有活动
        var lat = res.data.result.location.lat
        var lng = res.data.result.location.lng
        var sematic_description = res.data.result.sematic_description
        page.setData({
          city: city,
          lat: lat,
          lng: lng,
          sematic_description: sematic_description
        });

      },
      fail: function() {
        // fail  
      },
      complete: function() {
        // complete  
      }
    })
  },
  distance: function(la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    this.setData({
      s: s
    })
  },
  getAllzhuanjia: function(longitude, latitude) {
    var that = this
    var zid = that.data.zid
    t.util.request({
      'url': 'entry/wxapp/Zhuanjia.zhuanjiajinw',
      data: {
        jingdu: longitude,
        latitude: latitude,
        zid: zid,
        op: 'post'
      },
      success: function(res) {
        console.log(res)
        var info = res.data
        that.distance(latitude, longitude, info.lat, info.lng)
        info.juli = that.data.s;
        that.setData({
          zhuanjia: info
        })
      },
      fail: function(err) {
        console.log(err)
      },
    })
  },
  //踢出
  tichu(e) {
    var that = this
    var yao_id = that.data.yao_id
    t.util.request({
      'url': 'entry/wxapp/Yaoqing.tichu',
      data: {
        yao_id: yao_id
      },
      success: function(res) {
        console.log(res)
        wx.showModal({
          content: '踢出成功',
        })
        wx.navigateBack({
          detail: 1
        })
      }
    })
  },
  yijujue() {
    var that = this
    var yao_id = that.data.yao_id
    var zid = that.data.zid
    wx.showModal({
      content: '是否发起邀请',
      success: function(res) {
        if (res.confirm) {
          t.util.request({
            'url': 'entry/wxapp/Yaoqing.upyihu',
            data: {
              yao_id: yao_id,
              zid: zid
            },
            success: function(res) {
              console.log(res)
            }
          })
        }
      }
    })
  }
});